<template>
    <el-dialog
    :model-value="modelValue"
    @closed='closed'
    title="选择更换主题颜色"
    width="20%"
  >
    <div class="center">
        <el-color-picker v-model="mColor" :predefine="predefineColors" />
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closed">关闭</el-button>
        <el-button type="primary" @click="comfirm">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { defineProps,ref,defineEmits } from 'vue';
import { useStore } from 'vuex'
import { generateNewStyle,writeNewStyle } from '@/utils/theme.js'

const store = useStore()

defineProps({
  modelValue: {
    type: Boolean,
    required: true
  }
})
const emits = defineEmits(['update:modelValue'])

// 默认颜色
const mColor = ref(store.getters.mainColor)
// 预定义色值
const predefineColors = [
  '#ff4500',
  '#ff8c00',
  '#ffd700',
  '#90ee90',
  '#00ced1',
  '#1e90ff',
  '#c71585',
  'rgba(255, 69, 0, 0.68)',
  'rgb(255, 120, 0)',
  'hsv(51, 100, 98)',
  'hsva(120, 40, 94, 0.5)',
  'hsl(181, 100%, 37%)',
  'hsla(209, 100%, 56%, 0.73)',
  '#c7158577'
]

/**
 * 关闭
 */
const closed = () => {
   emits('update:modelValue', false)
}
/**
 * 确定
 * 1. 修改主题色
 * 2. 保存最新的主题色
 * 3. 关闭 dialog
 */
const comfirm = async () => {

  // 获取主题色
  const newStyleText = await generateNewStyle(mColor.value)
  // 插入修改的主题是css
  writeNewStyle(newStyleText)
  // 保存最新颜色本地
  store.commit('theme/setMainColor',mColor.value)
  //关闭 dialog
  closed()
}
</script>

<style lang="scss" scoped>
.center{
    display: flex;
    justify-content: center;
}
</style>